//
//  LRJVideoListViewController.m
//  BreadTravel
//
//  Created by 李润嘉 on 15/12/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LRJVideoListViewController.h"
#import "LRJVideoListTableViewCell.h"
#import "LRJVideoDetailViewController.h"

@interface LRJVideoListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *myTableView;

@property (nonatomic, strong)NSMutableArray *myArray;

@property (nonatomic, strong)LRJVideoModel *myModel;

@end

@implementation LRJVideoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    //创建tableview
    [self createTableView];
    
    //初始化数组
    self.myArray = [NSMutableArray array];
    
    //注册cell
    [self.myTableView registerClass:[LRJVideoListTableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    //请求网络数据
    [self getData];
}

//返回
-(void)leftButtonAction:(UIBarButtonItem *)bar{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

#pragma mark -- 请求网络数据
- (void)getData
{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"加载中";
    
    NSString *url = [NSString stringWithFormat:@"http://lvshiv.saygoer.com/mainStations/wIzA3r/travelVideos"];
    [AFNetworkTool getUrl:url body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableDictionary *bigDic = result;
        NSMutableArray *contentArr = [bigDic objectForKey:@"content"];
        for (NSMutableDictionary *dic in contentArr) {
            self.myModel  = [LRJVideoModel modelWithDictionary:dic];
            [self.myArray addObject:self.myModel];
            
        }
        [hub hide:YES];
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
       
        NSLog(@"error = %@", error);
        
    }];
    
}
#pragma mark -- 创建tableview
- (void)createTableView
{
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.rowHeight = 240;
    [self.view addSubview:self.myTableView];
}

#pragma mark -- tableview必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消置灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    static NSString *cellIndentifier = @"Cell";
    LRJVideoListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    cell.videoModel = [self.myArray objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark -- 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LRJVideoDetailViewController *videoDetailVC = [[LRJVideoDetailViewController alloc] init];
    [self.navigationController pushViewController:videoDetailVC animated:YES];
     videoDetailVC.myId = [[self.myArray objectAtIndex:indexPath.row] modelId];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
